package b3.widget.warehouse
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.build.*;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.WidgetManager;
	import b3.widget.basic.*;
	import b3.widget.card.CardItem;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class ObjectItem extends Sprite
	{
		public var obj:Obj;
		protected var imgSprite:Sprite;
		
		public function ObjectItem(obj:Obj)
		{
			super();
			this.obj = obj;
			imgSprite = new Sprite();
			//背景框
			var tabItemBackground:Sprite = new Sprite();
			tabItemBackground.graphics.beginFill(0xFFFFFF, 0);
			tabItemBackground.graphics.drawRect(0, 0, 92, 112);
			tabItemBackground.graphics.endFill();
			addChild(tabItemBackground);
			//标题名字
			var objectConfig:ObjConfig = Config.objConfigs[obj.objectIndex];
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
        	if (objectConfig.name.length < 6)
        	{
        		titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x0088FF, true);
        	}
        	else
        	{
        		titleText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0088FF, true);
        	}
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = objectConfig.name;
			titleText.x = (tabItemBackground.width - titleText.width) / 2;
			titleText.y = 85;
			addChild(titleText);
			addChild(imgSprite);
			refreshImg();
			//数量
			var numText:TextField = new TextField();
			numText.selectable = false;
        	numText.autoSize = TextFieldAutoSize.RIGHT;
    		numText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0088FF, true);
			numText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			numText.x = tabItemBackground.width - 10;
			numText.y = 5;
			numText.text = BFont.formatNumWithComa(obj.objectNum);
			addChild(numText);
			this.mouseChildren = false;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		protected function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
		}    
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}
		
		protected function onMouseClick(event:MouseEvent):void
		{
			var objectConfig:ObjConfig = Config.objConfigs[obj.objectIndex];
			var messageBox:MessageBox = new MessageBox("确定要使用 “" + objectConfig.name + "”？", true);
			messageBox.x = -messageBox.width / 2;
			messageBox.y = -messageBox.height / 2;
			messageBox.setOKCall(onOKButtonClick);
			WidgetManager.getInstance().popOut([messageBox], true);
		}
		
		/**
		 * 道具确认被使用
		 * 调用函数
		 */ 
		private function onOKButtonClick():void
		{
			var card:CardModel = new CardModel(obj.objectIndex, obj.objectNum);
			card.useCard();
			if (!(obj.objectIndex >= 300 && obj.objectIndex <= 307))
			{
				Warehouse.getInstance().close();
			}
		}

		protected function refreshImg():void
		{
			var ObjectImage:Class;
			var objectConfig:ObjConfig = Config.objConfigs[obj.objectIndex];
			ObjectImage = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage[0]);
			if (ObjectImage != null)
			{
				var img:Sprite = new ObjectImage();
				if (imgSprite.numChildren > 0)
					imgSprite.removeChildAt(0);
				img.width = 60;
				img.scaleY = img.scaleX;
				img.x = (this.width - img.width) / 2;
				img.y = 25;
				imgSprite.addChild(img);
			}
		}
	}
}